package cn.aezo.minions.gen.datasource;

import cn.aezo.core.entity.datasource.ColumnDefinition;
import cn.aezo.core.entity.datasource.ColumnInfo;
import cn.aezo.core.entity.datasource.DBConfig;
import cn.aezo.core.entity.datasource.TableDefinition;
import cn.aezo.core.entity.datasource.TableInfo;
import cn.aezo.minions.gen.datasource.mysql.MySqlDataSourceInfo;
import cn.aezo.minions.gen.model.ProjectConfig;
import cn.aezo.minions.utils.SqlHelper;
import cn.aezo.utils.base.ValidU;

import java.util.List;

/**
 * Created by smalle on 2017/12/14.
 */
public class DataSourceService {
    private ProjectConfig projectConfig;
    /**
     * 数据库连接信息
     */
    private SqlHelper sqlHelper;

    /**
     * 所有表信息
     */
    private TableInfo tableInfo;

    private DataSourceService() {}

    public static DataSourceService getInstance(ProjectConfig projectConfig) {
        DataSourceService dataSourceService = new DataSourceService();
        dataSourceService.projectConfig = projectConfig;
        dataSourceService.getSqlHelper();
        return dataSourceService;
    }

    public SqlHelper getSqlHelper() {
        if(sqlHelper == null && projectConfig != null) {
            String ip = projectConfig.getDatabaseIp();
            String port = projectConfig.getDatabasePort();
            String username = projectConfig.getDatabaseUsername();
            String password = projectConfig.getDatabasePassword();
            String driver = projectConfig.getDatabaseDriver();
            String dbName = projectConfig.getDatabaseDbName();

            DBConfig dbConfig = new DBConfig(ip, Integer.valueOf(port), username, password, driver, dbName);
            sqlHelper = SqlHelper.getInstance(dbConfig);
        }

        return sqlHelper;
    }

    public TableInfo getTableInfo() {
        if(tableInfo == null && sqlHelper != null) {
            SqlHelper sqlHelper = getSqlHelper();
            MySqlDataSourceInfo info = MySqlDataSourceInfo.getInstance(sqlHelper);
            tableInfo = info.getTableInfo();
        }

        return tableInfo;
    }

    /**
     * 获取某表信息
     * @param tableName
     * @return
     */
    public TableDefinition getTableDefinition(String tableName) {
        if(ValidU.isEmpty(tableName)) return null;

        List<TableDefinition> tables = tableInfo.getTableDefinitions(sqlHelper.getDbConfig().getDbName());
        for (TableDefinition table : tables) {
            if(tableName.equals(table.getTableName())) {
                return table;
            }
        }

        return null;
    }

    /**
     * 获取某表所有字段信息
     */
    public List<ColumnDefinition> getColumnDefinitions(String tableName) {
        if(ValidU.isEmpty(tableName)) return null;

        ColumnInfo columnInfo =  tableInfo.getColumnInfo();
        List<ColumnDefinition> columns = columnInfo.getColumnDefinitions(tableName);
        return columns;
    }

    /**
     * 获取某表某个字段信息
     */
    public ColumnDefinition getColumnDefinition(String tableName, String columnName) {
        if(ValidU.isEmpty(tableName) || ValidU.isEmpty(columnName)) return null;

        ColumnInfo columnInfo =  tableInfo.getColumnInfo();
        List<ColumnDefinition> columns = columnInfo.getColumnDefinitions(tableName);
        for (ColumnDefinition column : columns) {
            if(columnName.equals(column.getColumnName())) {
                return column;
            }
        }

        return null;
    }
}
